<template>
  <div>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/' }">后台管理</el-breadcrumb-item>
      <el-breadcrumb-item><a href="/">添加模板</a></el-breadcrumb-item>
    </el-breadcrumb>

    <el-divider></el-divider>

    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item label="名称" prop="name">
        <el-input v-model="ruleForm.name"></el-input>
      </el-form-item>
      <el-form-item label="拼音" prop="description">
        <el-input v-model="ruleForm.pinyin"></el-input>
      </el-form-item>
      <el-form-item label="关键字" prop="description">
        <el-input v-model="ruleForm.keywords"></el-input>
      </el-form-item>
      <el-form-item label="排序序号" prop="sort">
        <el-input v-model="ruleForm.sort"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">添加</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
export default {
  data() {
    return {
      ruleForm: {
        name: '测试数据001',
        pinyin: '测试数据的简介拼音：pinyin',
        keywords: '强',
        sort: '99'
      },
      rules: {
        name: [
          {required: true, message: '请输入模板名称', trigger: 'blur'},
          {min: 4, max: 15, message: '长度在 4 到 15 个字符', trigger: 'blur'}
        ],
        pinyin: [
          {required: true, message: '请输入模板拼音', trigger: 'blur'},
          {min: 2, max: 20, message: '长度在 2 到 20 个字符', trigger: 'blur'}
        ],
        keywords: [
          {required: true, message: '请输入模板关键字', trigger: 'blur'},
          {min: 2, max: 35, message: '长度在 4 到 35 个字符', trigger: 'blur'}
        ],
        sort: [
          {required: true, message: '请输入排序序号', trigger: 'blur'},
          {pattern: /^(\d{1}|[1-9]{1}[0-9]?)$/, message: '排序序号必须是 0~99 之间的数字', trigger: 'blur'}
        ]
      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
            let url = 'http://localhost:8080/attributeTemplate/addNew';
            console.log('url = ' + url);

            let formdata = this.qs.stringify(this.ruleForm);
            console.log('formdata = ' + formdata);
            this.axios
                .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
                .post(url, formdata).then((response) => {
              let responsebody = response.data;
              if (responsebody.code == 20000) {
                this.$message({
                  showclose: true,
                  message: '添加模板成功！',
                  type: 'success'
                });
                this.resetForm(formName);
              } else if (responsebody.code == 40900) {
                this.$message({
                  showclose: true,
                  message: responsebody.message,
                  type: 'error'
                });
              }
            });
          }
      )
      ;
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
}
</script>